package ren.xbcr.schoolbus.client.service;

import android.content.Context;

import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import ren.xbcr.schoolbus.client.domain.AppInfo;
import ren.xbcr.schoolbus.client.domain.BusDriver;
import ren.xbcr.schoolbus.client.domain.ClientServerSync;
import ren.xbcr.schoolbus.client.domain.DriverRating;
import ren.xbcr.schoolbus.client.domain.LoginUser;
import ren.xbcr.schoolbus.client.domain.RecruitDriver;
import ren.xbcr.schoolbus.client.domain.transmission.ResponseResult;
import ren.xbcr.schoolbus.client.domain.transmission.ResponseResultWithRecruitDriver;
import ren.xbcr.schoolbus.client.util.Constants;

/**
 * Created by Jian on 2015/10/31.
 */
public class SchoolBusService {

    private Context context;

    public SchoolBusService(Context context) {
        this.context = context;
    }

    public SchoolBusService() {
    }

    public ArrayList<BusDriver> getAllBusDriver(LoginUser loginUser) {
        try {
            RestTemplate restTemplate = new RestTemplate();
            restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
            ArrayList<BusDriver> busDrivers = new ArrayList<BusDriver>(Arrays.asList(restTemplate.postForObject(Constants.GET_ALL_DRIVER_URL, loginUser, BusDriver[].class)));
            return busDrivers;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public ArrayList<String> getAllSchool() {
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
        ArrayList<String> schools = new ArrayList<String>(Arrays.asList(restTemplate.getForObject(Constants.GET_SCHOOLS_URL, String[].class)));
        return schools;
    }

    public Boolean isValidPhone(String validCode) {
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
        Map<String, String> keyValue = new HashMap<String, String>();
        keyValue.put(Constants.VALID_CODE, validCode);
        Boolean isValidPhone = restTemplate.getForObject(Constants.GET_ALL_DRIVER_URL, Boolean.class, keyValue);
        return isValidPhone;
    }

    public Boolean loginOrRegister(LoginUser loginUser) {
        try {
            RestTemplate restTemplate = new RestTemplate();
            restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
            Boolean isValidUser = restTemplate.postForObject(Constants.LOGIN_OR_REGISTER_URL, loginUser, Boolean.class);
            return isValidUser;

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public void sendVerifyCode(String telephoneNumber) {
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
        LoginUser loginUser = new LoginUser();
        loginUser.setTelephoneNumber(telephoneNumber);
        restTemplate.postForObject(Constants.SEND_VERIFY_CODE_URL, loginUser, Boolean.class);
    }

    public ResponseResult callCar(LoginUser loginUser) {
        try {
            RestTemplate restTemplate = new RestTemplate();
            restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
            ResponseResult responseResult = restTemplate.postForObject(Constants.CALL_CAR_URL, loginUser, ResponseResult.class);

            return responseResult;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public ClientServerSync clientSyncServer(LoginUser loginUser) {
        try {
            RestTemplate restTemplate = new RestTemplate();
            restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
            ClientServerSync clientServerSync = restTemplate.postForObject(Constants.CLIENT_SYNC_SERVER_URL, loginUser, ClientServerSync.class);
            return clientServerSync;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public void postDriverRating(DriverRating driverRating) {
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
        restTemplate.postForObject(Constants.POST_DRIVER_RATING_URL, driverRating, Boolean.class);
    }

    public Boolean recruitDriver(RecruitDriver mRecruitDriver) {
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
        Boolean isValidUser = restTemplate.postForObject(Constants.RECRUIT_DRIVER_URL, mRecruitDriver, Boolean.class);
        return isValidUser;
    }

    public ResponseResultWithRecruitDriver fetchExistRecruit(LoginUser loginUser) {
        try {
            RestTemplate restTemplate = new RestTemplate();
            restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
            ResponseResultWithRecruitDriver result = restTemplate.postForObject(Constants.FETCH_EXIST_RECRUIT_DRIVER_URL, loginUser, ResponseResultWithRecruitDriver.class);
            return result;
        } catch (Exception e) {
            throw new RuntimeException(e);
//            return null;
        }
    }

    public AppInfo getVersionCode() {
        try {
            RestTemplate restTemplate = new RestTemplate();
            restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
            AppInfo appInfo = restTemplate.postForObject(Constants.GET_VERSION_CODE, new LoginUser(), AppInfo.class);
            return appInfo;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
